<?php

namespace app\index\controller;
use think\Db;
class Course extends Base
{
    //显示课程页面
    public function index(){
        return $this->fetch();
    }
    //显示课程页面
    public function List(){
       if($this->request->param('status')==1){
            $where=['is_putaway'=>1,'status_id'=>1];
            $is_putaway=1;
       }elseif($this->request->param('status')==2){
            $where=['is_putaway'=>2,'status_id'=>1];
            $is_putaway=2;
       }else{
        $where=['is_putaway'=>[1,2],'status_id'=>1];
       }
        $res=Db::table('course')->field('id,course_name,is_free,original_price,is_putaway')->where($where)->order('create_time','desc')->select();
        if($res){
            $msg=['code'=>200,'data'=>$res];
        }else{
            $msg=['code'=>500];
        }
        return json($msg);
    }
    // 添加课程页面
    public function add(){
        $subject=$this->getSubject();
        $this->assign('subject',$subject);
    	return $this->fetch();
    }
    // 添加课程
    public function addFunc()
    {
//        var_dump($this->request->param());die;
        $data = $this->request->param();
        $data['lecturer_user_no'] = intval(session('lecturer_user_no'));
//        $data['id']=time().mt_rand(100,999);
        $data['create_time'] = date('Y-m-d H:i:s', time());
        $data['edit_time'] = date('Y-m-d H:i:s', time());
        $data['status_id'] = 1;
        $data['update_person_id'] = 1;
        $data['current_price'] = null;
        $data['is_putaway'] = 1;
        $data['count_buy'] = 0;
        $data['count_study'] = 0;
        $data['period_total'] = 0;
        $data['service_period'] = null;
        $data['course_sort'] = 0;
        $data['keywords'] = '';//管理员设置，后期添加
        $data['sort'] = 1;//管理员设置，后期添加
        $res = Db::table('course')->strict(false)->insertGetId($data);
        $url = url('index/course/chapter', ['id' => $res]);
        if ($res) {
            echo '<script>alert("添加成功");window.location.href="' . $url . '"</script>';
        } else {
            $this->error('信息有误', 'index/course/add');
        }
    }

    // 编辑课程
    public function editCourse(int $id){
//        echo '页面';
        if($this->request->param('type')==1){
            $where=['status_id'=>1,'id'=>$id];
            $res=Db::table('course')->field('id,course_level,is_free,subject_id,course_name,course_logo,introduce,original_price')->where($where)->find();
            return json($res);
        }elseif($this->request->param('type')==2){
            $subject=$this->getSubject();
            return json($subject);
        }else{
            $this->assign('id',$id);
            return $this->fetch('edit');
        }
    }
    // 编辑课程方法
    public function editCourseFunc(int $id){
        $data = $this->request->param();
        $res = Db::table('course')->where('id',$id)->update($data);
        if($res){
            return json(['data'=>['code'=>200,'msg'=>'ok']]);
        }else{
            return json(['data'=>['code'=>500]]);
        }


    }
    // 上下架课程
    public function putCourseFunc(int $id){
        $is_putaway = $this->request->param('is_putaway');
        $data['is_putaway'] =$is_putaway;
        $data['edit_time'] = date('Y-m-d H:i:s', time());
        $res = Db::table('course')->strict(false)->where('id',$id)->update($data);
        if($res){
            $msg=['code'=>200,'data'=>$res];
        }else{
            $msg=['code'=>500];
        }
        return json($msg);
        
    }
    // 添加章节页面
    public function chapter(int $id){
        $this->assign('id',$id);
    	return $this->fetch();
    }
    // 章节列表
    public function chapterList(int $id){
        $res=Db::table('course_chapter')->field('id,sort,chapter_name,chapter_desc')->where(['course_id'=>$id,'status_id'=>1])->order('sort','asc')->select();
        $data=['code'=>200,'data'=>$res];
        return json($data);
    }
    // 添加章节方法
    public function addChapter(int $id){
        $data = $this->request->param();
        unset($data['id']);
        unset($data['courseID']);
        $data['create_time'] = date('Y-m-d H:i:s', time());
        $data['edit_time'] = date('Y-m-d H:i:s', time());
        $data['status_id'] = 1;
        $data['course_id']=$id;
        $res = Db::table('course_chapter')->strict(false)->insertGetId($data);
        if($res){
            $msg=['code'=>200,'data'=>$res];
        }else{
            $msg=['code'=>500];
        }
        return json($msg);
    }
    // 编辑章节方法
    public function editChapter(int $id){
        $data = $this->request->param();
        unset($data['id']);
        $data['edit_time'] = date('Y-m-d H:i:s', time());
        $data['status_id'] = 1;
        $res = Db::table('course_chapter')->strict(false)->where('id',$id)->update($data);
        if($res){
            $msg=['code'=>200,'data'=>$res];
        }else{
            $msg=['code'=>500];
        }
        return json($msg);
    }
    // 删除章节方法
    public function delChapter(int $id){
        $data['status_id'] = 2;
        $res = Db::table('course_chapter')->where('id',$id)->update($data);
        if($res){
            $msg=['code'=>200,'data'=>$res];
        }else{
            $msg=['code'=>500];
        }
        return json($msg);
    }
    // 添加课时页面
    public function period(int $id){
        $res=DB::table('course_chapter')->where('id',$id)->field('chapter_name,course_id')->find();
        $this->assign('courseId',$res['course_id']);
        $this->assign('chapterId',$id);
        $this->assign('chapter_name',$res['chapter_name']);
        return $this->fetch();
    }
    // 课时列表
    public function periodList(int $id){
        $res=Db::table('course_chapter_period')->field('id,sort,course_id,chapter_id,period_name,is_free,is_video,video_vid,is_doc,doc_name,doc_url')->where(['chapter_id'=>$id,'status_id'=>1])->order('sort','asc')->select();
        $data=['code'=>200,'data'=>$res];
        return json($data);
    }
    // 添加课时方法
    public function addPeriod(int $id){
        $data = $this->request->param();
        unset($data['id']);
        $data['create_time'] = date('Y-m-d H:i:s', time());
        $data['edit_time'] = date('Y-m-d H:i:s', time());
        $data['status_id'] = 1;
        $res = Db::table('course_chapter_period')->strict(false)->insertGetId($data);
        if($res){
            $msg=['code'=>200,'data'=>$res];
        }else{
            $msg=['code'=>500];
        }
        return json($msg);
    }
    // 编辑课时方法
    public function editPeriod(int $id){
        $data = $this->request->param();
        unset($data['id']);
        $data['edit_time'] = date('Y-m-d H:i:s', time());
        $data['status_id'] = 1;
        $res = Db::table('course_chapter_period')->strict(false)->where('id',$id)->update($data);
        if($res){
            $msg=['code'=>200,'data'=>$res];
        }else{
            $msg=['code'=>500];
        }
        return json($msg);
    }
    // 删除课时方法
    public function delPeriod(int $id){
        $data['status_id'] = 2;
        $res = Db::table('course_chapter_period')->where('id',$id)->update($data);
        if($res){
            $msg=['code'=>200,'data'=>$res];
        }else{
            $msg=['code'=>500];
        }
        return json($msg);
    }
}
